package com.example.replacebooks.service;

import cn.hutool.core.date.DateUtil;
import com.example.replacebooks.common.JwtTokenUtils;
import com.example.replacebooks.entity.Bookinfo;
import com.example.replacebooks.entity.Params;
import com.example.replacebooks.entity.User;
import com.example.replacebooks.exception.CustomException;
import com.example.replacebooks.mapper.BookinfoMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class BookinfoService {
    @Resource
    private BookinfoMapper bookinfoMapper;

    public void add(Bookinfo bookinfo) {
        // 进行重复性判断，同一id的书籍修改其内容
        Bookinfo bookid = bookinfoMapper.findByBookid(bookinfo.getBookid());
        if (bookid != null){
            // 该id已存在
            bookinfoMapper.updateBybookid(bookinfo);
        }else{
            // 加入发布者id
            User currentUser = JwtTokenUtils.getCurrentUser();
            Integer userid = currentUser.getUserid();
            bookinfo.setUserid(userid);

            bookinfoMapper.insertSelective(bookinfo);
        }
    }

    // 通过审核
    public PageInfo<Bookinfo> findBySearch(Params params) {
        User currentUser = JwtTokenUtils.getCurrentUser();
        Integer userid = currentUser.getUserid();
        Integer role = currentUser.getRole();
        // 开启分页查询
        PageHelper.startPage(params.getPageNum(),params.getPageSize());
        // 接下来的查询会自动按照当前开启的分页查询
        List<Bookinfo> list = bookinfoMapper.findBySearch(params,userid,role);
        return PageInfo.of(list);
    }

    // 通过用户id查找自身的所有书籍信息
    public PageInfo<Bookinfo> myaddbook(Params params) {
        User currentUser = JwtTokenUtils.getCurrentUser();
        Integer userid = currentUser.getUserid();
        Integer role = currentUser.getRole();
        // 开启分页查询
        PageHelper.startPage(params.getPageNum(),params.getPageSize());
        // 接下来的查询会自动按照当前开启的分页查询
        List<Bookinfo> list = bookinfoMapper.myaddbook(params,userid,role);
        return PageInfo.of(list);
    }

    //未通过审核
    public PageInfo<Bookinfo> findByStatuSearch(Params params) {
        User currentUser = JwtTokenUtils.getCurrentUser();
        Integer userid = currentUser.getUserid();
        Integer role = currentUser.getRole();
        // 开启分页查询
        PageHelper.startPage(params.getPageNum(),params.getPageSize());
        // 接下来的查询会自动按照当前开启的分页查询
        List<Bookinfo> list = bookinfoMapper.findByStatuSearch(params,userid,role);
        return PageInfo.of(list);
    }

    public void delete(Integer bookid) {
        bookinfoMapper.deleteByPrimaryKey(bookid);
    }

    public Bookinfo findByBookid(Integer bookid) {
        Bookinfo list = bookinfoMapper.findByBookid(bookid);
        return list;
    }

    public List<Bookinfo> findByClassid(Integer classid) {
        List<Bookinfo> list = bookinfoMapper.findByClassid(classid);
        return list;
    }

    public List<Bookinfo> findByBookids(Integer bookid) {
        List<Bookinfo> list = bookinfoMapper.findByBookids(bookid);
        return list;
    }

    public void savestatu(Bookinfo bookinfo) {
        // 更新审核状态和审核时间
        bookinfo.setStatu("已通过");
        bookinfo.setTime(DateUtil.now());
        bookinfoMapper.updateBybookid(bookinfo);
    }
}
